import http from '../../utils/http';
import {myFectch} from '../../utils/useFetch';
import {showAccount, showRole} from '../../api/role';
const Role = ()=>{
    return {
        caidan: [            
            {
                id: 0,
                icon: 'ReadOutlined',
                label:'首页',
                url: '/home/shouye'
            },
            {
                id: 1,
                icon: 'UserOutlined',
                label: '会员管理',
                children: [
                    {
                        id: 2,
                        txt: '会员管理',
                        url: '/home/MembershipManagement'
                    },
                    {
                        id: 3,
                        txt: '会员标签管理',
                        url: '/home/MemberLabelManagement'
                    },
                    {
                        id: 4,
                        txt: '优惠券管理',
                        url: '/home/CouponManagement'
                    },
                    {
                        id: 20,
                        txt: '会员营销',
                        url: '/home/HuiManagement'
                    }
                ]
            },
            {
                id: 5,
                icon: 'ReadOutlined',
                label: '内容管理',
                children: [
                    {
                        id: 6,
                        txt: '内容管理',
                        url: '/home/contentManagement'
                    },
                    {
                        id: 19,
                        txt: '频道管理', 
                        url: '/home/contentPinManagement'
                    },
                    {
                        id: 7,
                        txt: '内容标签管理',
                        url: '/home/contentLabelManagement'
                    },
                    {
                        id: 8,
                        txt: '作者管理',
                        url: '/home/authorManagement'
                    },
                    {
                        id: 9,
                        txt: '勋章管理',
                        url: '/home/medalManagement'
                    }
                ]
            },
            
            {
                id: 10,
                icon: 'FileTextOutlined',
                label: '订单管理',
                children: [
                    {
                        id: 11,
                        txt: '订单管理',
                        url: '/home/orderManagement'
                    }
                ]
            },
            {
                id: 12,
                icon: 'ShopOutlined',
                label: '积分商城管理',
                children: [
                    {
                        id: 13,
                        txt: '运营位管理',
                        url: '/home/operateManagement'
                    },
                    {
                        id: 14,
                        txt: '商品管理',
                        url: '/home/goodsManagement'
                    },
                    {
                        id: 15,
                        txt: '商品类目管理',
                        url: '/home/mallcategory'
                    }
                ]
            },
            {
                id: 16,
                icon: 'SettingOutlined',
                label: '系统管理',
                children: [
                    {
                        id: 17,
                        txt: '帐号管理',
                        url: '/home/system'
                    },
                    {
                        id: 18,
                        txt: '角色管理',
                        url: '/home/role'
                    }
                ]
            },
            {
                id: 21,
                icon: 'HomeOutlined',
                label: '加盟商管理',
                children: [
                    {
                        id: 22,
                        txt: '加盟商管理',
                        url: '/home/Jiameng'
                    },
                    {
                        id: 23,
                        txt: '财务结算',
                        url: '/home/JiaCai'
                    }
                ]
            },
            {
                id: 24,
                icon: 'ToolOutlined',
                label: '售后维护',
                children: [
                    {
                        id: 25,
                        txt: 'SN码管理',
                        url: '/home/SNmanagement'
                    }
                ]
            }
        ],
        ceshi:[],
        allMenu:[],
        twoMenu:[],
        roleData: [],
        renyuan:[],
        // 登录人员可查看的菜单
        // 获取一级菜单
        async getOneMenu(){
            let a = sessionStorage.getItem('oneMenu');
            a = JSON.parse(a);
            let temp=[
                {
                id: 0,
                icon: 'ReadOutlined',
                label:'首页',
                url: '/home/shouye'
            }
        ];
            for(let i=0;i<this.caidan.length;i++){
                for(let j=0;j<a.length;j++){
                    if(this.caidan[i].id===a[j]){
                        temp.push(this.caidan[i])
                    }
                }
            }
            this.ceshi = temp;
            sessionStorage.setItem('menu',JSON.stringify(temp));
        },
        getAllTwoMenu(){
            http({url:'/menuRole/showTwo',method:'post'}).then(
                data=>{
                    this.twoMenu = data.data;
                }
            )
        },
        // 获取人员
        async getRen(){
            const m = await showAccount();
            this.renyuan = m.data;
        },
        // 获取角色
        async getRoles(){
            const m = await showRole();
            this.roleData = m.data;
        },
        // 登录人员
        loginper:{},
        getLogin(){
            this.loginper = JSON.parse(sessionStorage.getItem('userInfo'));
            // console.log(this.loginper);
        },
        // 重置密码
        setPwd(val){
            console.log(val);
            return http({url: '/admin/updatePwd',params:{
                username:val.userName,
                pwd: val.password,
                pwd2: val.confirm
            },method:'POST'})
        },
        // 人员
        async addRen(val){
            const value = {
                email: val.email,
                name: val.name,
                phone: val.phone,
                pwd: val.pwd,
                status: val.status,
                username: val.username
            }
            return await myFectch({url:'/admin/add?role_id='+val.role_id,
            data:value,
            method:'post',
            headers:'application/json'})
        },
        changeRen(id,val){  
            let value={
                email: val.email,
                id: val.id,
                name: val.name,
                phone: val.phone,
                username: val.username
            }
            return myFectch({url:'/admin/updateAdmin?role_id='+id,
            method:'post',
            data:value,
            headers:'application/json'})
        },
        delRen(val){
            for(let i=0;i<this.renyuan.length;i++){
                if(val === this.renyuan[i].id){
                    this.renyuan.splice(i,1)
                }
            }
            return http({url:'/admin/deleteById',method:'POST',params: {id:val}})
        },
        // 人员冻结
        statusFre(val){
            return myFectch({url:'/admin/freezeById',
            method:'post',
            data:val,
            headers:'application/json'})
        },
        // 人员解冻
        statusUnfre(val){
            return myFectch({url:'/admin/unfreezeById',
            method:'post',
            data:val,
            headers:'application/json'})
        },
        // 搜索
        async searchAll(str){
            http({url: '/admin/select',method:'POST',params:{str}}).then(data=>{
                    console.log(data);
                    if(data.code===200){
                        this.renyuan = data.data
                    }                   
                }
            )
        },

        // 角色
        addRole(val){
            return http({url:'/admin/addRole',method:'post',params:val})
        },
        // 修改角色名称
        changeRole(val){
            return http({url: '/admin/updateRoleNameById',
            method:'post',
            params:{
                id:val.id,
                name: val.roleMark
            }})
        },
        // 修改角色权限
        changePower(id,val){
            let value = val.map(item=>{return {id: item.key}});
            return myFectch({url:'/menuRole/addPeermission?id='+id,
            method:'post',
            headers:'application/json',
            data:value})
        },
        delRole(val){
            return http({url:'/admin/deleteRole',method:'post',params:{id:val}})
        },
        freezeRole(val){
            return http({url:'/admin/freezeRoleById',method:'post',params:{id: val}})
        },
        unfreeRole(val){
            return http({url:'/admin/unFreezeRoleById',method:'post',params:{id: val}})
        },
        // 退出登录
        loginOut(){
            return http({url:'/empl/logout',method:'post'})
        }
    }
}
export default Role();